<?php
/*
 * $Id: LoggerTest.class.php 31 2009-05-09 10:54:26Z namba@craid.co.jp $
 * =============================================================================
 * Barge - Web application framework on PHP5 
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * =============================================================================
 * For more information, see http://barge.googlecode.com/
 */
namespace barge\logging;

use \UnitTestCase;

use barge\System;
use barge\logging\LogFactory;
use barge\utils\OutputBuffer;

/**
 * @see UnitTestCase
 */
class LoggerTest extends UnitTestCase 
{
	/**
	 * @test
	 */
	public function testDebug() 
	{
		System::setProperty('Logging/DebugLogger', 
			array('appenderClass' => 'barge\logging\ConsoleAppender', 
				'priority' => 'debug'));
		$logger = LogFactory::getLogger('DebugLogger');
		
		$this->assertTrue($logger->isDebugEnabled());
		$this->assertTrue(OutputBuffer::capture(
				function() use ($logger) {$logger->debug("MESSAGE");}) 
				!== null);
		$this->assertTrue($logger->isInfoEnabled());
		$this->assertTrue(OutputBuffer::capture(
				function() use ($logger) {$logger->info("MESSAGE");}) 
				!== null);
		$this->assertTrue($logger->isWarnEnabled());
		$this->assertTrue(OutputBuffer::capture(
				function() use ($logger) {$logger->warn("MESSAGE");}) 
				!== null);
		$this->assertTrue($logger->isErrorEnabled());
		$this->assertTrue(OutputBuffer::capture(
				function() use ($logger) {$logger->error("MESSAGE");}) 
				!== null);
		$this->assertTrue($logger->isFatalEnabled());
		$this->assertTrue(OutputBuffer::capture(
				function() use ($logger) {$logger->fatal("MESSAGE");}) 
				!== null);

	}
	
	/**
	 * @test
	 */
	public function testInfo() 
	{
		System::setProperty('Logging/InfoLogger', 
			array('appenderClass' => 'barge\logging\ConsoleAppender', 
				'priority' => 'info'));
		$logger = LogFactory::getLogger('InfoLogger');
		
		$this->assertTrue($logger->isDebugEnabled() === false);
		$this->assertTrue(OutputBuffer::capture(
				function() use ($logger) {$logger->debug("MESSAGE");}) 
				=== '');
		$this->assertTrue($logger->isInfoEnabled());
		$this->assertTrue(OutputBuffer::capture(
				function() use ($logger) {$logger->info("MESSAGE");}) 
				!== null);
		$this->assertTrue($logger->isWarnEnabled());
		$this->assertTrue(OutputBuffer::capture(
				function() use ($logger) {$logger->warn("MESSAGE");}) 
				!== null);
		$this->assertTrue($logger->isErrorEnabled());
		$this->assertTrue(OutputBuffer::capture(
				function() use ($logger) {$logger->error("MESSAGE");}) 
				!== null);
		$this->assertTrue($logger->isFatalEnabled());
		$this->assertTrue(OutputBuffer::capture(
				function() use ($logger) {$logger->fatal("MESSAGE");}) 
				!== null);

	}
	
	/**
	 * @test
	 */
	public function testWarn() 
	{
		System::setProperty('Logging/WarnLogger', 
			array('appenderClass' => 'barge\logging\ConsoleAppender', 
				'priority' => 'warn'));
		$logger = LogFactory::getLogger('WarnLogger');
		
		$this->assertTrue($logger->isDebugEnabled() === false);
		$this->assertTrue(OutputBuffer::capture(
				function() use ($logger) {$logger->debug("MESSAGE");}) 
				=== '');
		$this->assertTrue($logger->isInfoEnabled() === false);
		$this->assertTrue(OutputBuffer::capture(
				function() use ($logger) {$logger->info("MESSAGE");}) 
				=== '');
		$this->assertTrue($logger->isWarnEnabled());
		$this->assertTrue(OutputBuffer::capture(
				function() use ($logger) {$logger->warn("MESSAGE");}) 
				!== null);
		$this->assertTrue($logger->isErrorEnabled());
		$this->assertTrue(OutputBuffer::capture(
				function() use ($logger) {$logger->error("MESSAGE");}) 
				!== null);
		$this->assertTrue($logger->isFatalEnabled());
		$this->assertTrue(OutputBuffer::capture(
				function() use ($logger) {$logger->fatal("MESSAGE");}) 
				!== null);

	}
	
	/**
	 * @test
	 */
	public function testError() 
	{
		System::setProperty('Logging/ErrorLogger', 
			array('appenderClass' => 'barge\logging\ConsoleAppender', 
				'priority' => 'error'));
		$logger = LogFactory::getLogger('ErrorLogger');
		
		$this->assertTrue($logger->isDebugEnabled() === false);
		$this->assertTrue(OutputBuffer::capture(
				function() use ($logger) {$logger->debug("MESSAGE");}) 
				=== '');
		$this->assertTrue($logger->isInfoEnabled() === false);
		$this->assertTrue(OutputBuffer::capture(
				function() use ($logger) {$logger->info("MESSAGE");}) 
				=== '');
		$this->assertTrue($logger->isWarnEnabled() === false);
		$this->assertTrue(OutputBuffer::capture(
				function() use ($logger) {$logger->warn("MESSAGE");}) 
				=== '');
		$this->assertTrue($logger->isErrorEnabled());
		$this->assertTrue(OutputBuffer::capture(
				function() use ($logger) {$logger->error("MESSAGE");}) 
				!== null);
		$this->assertTrue($logger->isFatalEnabled());
		$this->assertTrue(OutputBuffer::capture(
				function() use ($logger) {$logger->fatal("MESSAGE");}) 
				!== null);

	}
	
	
	/**
	 * @test
	 */
	public function testFatal() 
	{
		System::setProperty('Logging/FatalLogger', 
			array('appenderClass' => 'barge\logging\ConsoleAppender', 
				'priority' => 'fatal'));
		$logger = LogFactory::getLogger('FatalLogger');
		
		$this->assertTrue($logger->isDebugEnabled() === false);
		$this->assertTrue(OutputBuffer::capture(
				function() use ($logger) {$logger->debug("MESSAGE");}) 
				=== '');
		$this->assertTrue($logger->isInfoEnabled() === false);
		$this->assertTrue(OutputBuffer::capture(
				function() use ($logger) {$logger->info("MESSAGE");}) 
				=== '');
		$this->assertTrue($logger->isWarnEnabled() === false);
		$this->assertTrue(OutputBuffer::capture(
				function() use ($logger) {$logger->warn("MESSAGE");}) 
				=== '');
		$this->assertTrue($logger->isErrorEnabled() === false);
		$this->assertTrue(OutputBuffer::capture(
				function() use ($logger) {$logger->error("MESSAGE");}) 
				=== '');
		$this->assertTrue($logger->isFatalEnabled());
		$this->assertTrue(OutputBuffer::capture(
				function() use ($logger) {$logger->fatal("MESSAGE");}) 
				!== null);

	}
	
	/**
	 * @test
	 */
	public function testDisabled() 
	{
		System::setProperty('Logging/DisabledLogger', 
			array('appenderClass' => 'barge\logging\ConsoleAppender', 
				'priority' => ''));
		$logger = LogFactory::getLogger('DisabledLogger');
		
		$this->assertTrue($logger->isDebugEnabled() === false);
		$this->assertTrue(OutputBuffer::capture(
				function() use ($logger) {$logger->debug("MESSAGE");}) 
				=== '');
		$this->assertTrue($logger->isInfoEnabled() === false);
		$this->assertTrue(OutputBuffer::capture(
				function() use ($logger) {$logger->info("MESSAGE");}) 
				=== '');
		$this->assertTrue($logger->isWarnEnabled() === false);
		$this->assertTrue(OutputBuffer::capture(
				function() use ($logger) {$logger->warn("MESSAGE");}) 
				=== '');
		$this->assertTrue($logger->isErrorEnabled() === false);
		$this->assertTrue(OutputBuffer::capture(
				function() use ($logger) {$logger->error("MESSAGE");}) 
				=== '');
		$this->assertTrue($logger->isFatalEnabled() === false);
		$this->assertTrue(OutputBuffer::capture(
				function() use ($logger) {$logger->fatal("MESSAGE");}) 
				=== '');

	}
	
}

?>